package com.example.alladapter.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.alladapter.Datas.PicName;
import com.example.alladapter.R;

import java.util.ArrayList;

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.viewHolder> {

    private Context context;
    private OnRecycleClickListen onRecycleClickListen;
    private int type;
    private ArrayList<PicName> arrayListpic;

    /**
     * @param type                 -1 不传入数据
     *                             0 ArrayList<String>
     *                             1 ArrayList<其他类型>
     *                             2 *********
     *                             3 ************
     * @param context
     * @param onRecycleClickListen
     */
    public RecycleViewAdapter(int type, Context context, OnRecycleClickListen onRecycleClickListen) {
        this.context = context;
        this.type = type;
        this.onRecycleClickListen = onRecycleClickListen;

    }

    public RecycleViewAdapter(int type, Context context, OnRecycleClickListen onRecycleClickListen, ArrayList<PicName> arrayList) {
        this.context = context;
        this.onRecycleClickListen = onRecycleClickListen;
        this.type = type;
        this.arrayListpic = arrayList;
    }

    @NonNull
    @Override
    public RecycleViewAdapter.viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        viewHolder ViewHolder = null;
        switch (type) {
            case -1:
                ViewHolder = new viewHolder(LayoutInflater.from(context).inflate(R.layout.recycleview_item, parent, false));
                break;
            case 0:
                ViewHolder = new viewHolder(LayoutInflater.from(context).inflate(R.layout.recycleview_muen_item, parent, false));
                break;
            case 1:
                ViewHolder = new viewHolder(LayoutInflater.from(context).inflate(R.layout.recycle_pic_item, parent, false));
        }
        return ViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecycleViewAdapter.viewHolder holder, int position) {
        switch (type) {
            case -1:
                ArrayList<String> arrayList1 = new ArrayList<>();
                String str = "登鹳雀楼，白日依山尽，黄河入海流。";
                arrayList1.clear();
                for (int i = 0; i < 50; i++) {
                    str += i;
                    arrayList1.add(str);
                }
                holder.sedate(arrayList1.get(position));
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onRecycleClickListen.Onclick(position);
                    }
                });
                break;
            case 0:
                holder.setDates(arrayListpic.get(position).string);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onRecycleClickListen.Onclick(position);
                    }
                });
                break;
            case 1:
                holder.setpicDatas(arrayListpic.get(position));
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onRecycleClickListen.Onclick(position);
                    }
                });
                break;
        }


    }

    @Override
    public int getItemCount() {
        int counts = 0;
        switch (type) {
            case -1:
                counts = 50;
                break;
            case 0:
            case 1:
                counts = arrayListpic.size();
                break;
        }
        return counts;
    }

    public interface OnRecycleClickListen {
        /***
         *
         * @param pos 位置
         */
        void Onclick(int pos);

    }

    class viewHolder extends RecyclerView.ViewHolder {
        private TextView tv_top_content, tv_menu, tv_word;
        private ImageView imageView;

        public viewHolder(@NonNull View itemView) {
            super(itemView);
            switch (type) {
                case -1:
                    tv_top_content = itemView.findViewById(R.id.rec_item_top);
                    break;
                case 0:
                    tv_menu = itemView.findViewById(R.id.tv_menu_list);
                    break;
                case 1:
                    tv_word = itemView.findViewById(R.id.pic_word);
                    imageView = itemView.findViewById(R.id.pic_img);
            }

        }

        public void sedate(String string) {
            tv_top_content.setText(string);
        }

        public void setDates(String s) {
            tv_menu.setText(s);
        }

        public void setpicDatas(PicName picName) {
            imageView.setImageResource(picName.pic);
            tv_word.setText(picName.string);
        }
    }
}
